package com.landiao.common.weixin.bean;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

import com.landiao.common.weixin.WxConsts;

/**
 * 分组群发的消息
 * 
 * @author chanjarster
 */
public class WxMpMassGroupMessage implements Serializable {

	private String groupId;
	private String msgtype;
	private String content;
	private String mediaId;

	public WxMpMassGroupMessage() {
		super();
	}

	public String getMsgtype() {
		return msgtype;
	}

	/**
	 * <pre>
	 * 请使用
	 * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_IMAGE}
	 * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_NEWS}
	 * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_TEXT}
	 * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VIDEO}
	 * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VOICE}
	 * 如果msgtype和media_id不匹配的话，会返回系统繁忙的错误
	 * </pre>
	 * @param msgtype
	 */
	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMediaId() {
		return mediaId;
	}

	public void setMediaId(String mediaId) {
		this.mediaId = mediaId;
	}

	/**
	 * {
   "filter":{
      "is_to_all":false
      "group_id":"2"
   },
   "mpnews":{
      "media_id":"123dsdajkasd231jhksad"
   },
    "msgtype":"mpnews"
	}
	 * @return
	 */
	public String toJson() {
		StringBuilder sb = new StringBuilder();
		sb.append("{\"filter\":{\"is_to_all\":");
		if(StringUtils.isNotEmpty(groupId)){
			sb.append(false).append(",\"group_id\":\"").append(groupId).append("\"},");
		}else{
			sb.append(true).append("},");
		}
		switch (msgtype) {
		case WxConsts.MASS_MSG_NEWS:
		case WxConsts.MASS_MSG_VOICE:
		case WxConsts.MASS_MSG_IMAGE:
		case WxConsts.MASS_MSG_VIDEO:
			sb.append("\"").append(msgtype).append("\":{\"media_id\":\"").append(mediaId).append("\"},");
			break;
		case WxConsts.MASS_MSG_TEXT:
			sb.append("\"").append(msgtype).append("\":{\"content\":\"").append(content).append("\"},");
			break;
		}
		sb.append("\"msgtype\":\"").append(msgtype).append("\"}");
		return sb.toString();
	}

	public String getGroupId() {
		return groupId;
	}

	/**
	 * 如果不设置则就意味着发给所有用户
	 * @param groupId
	 */
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

}
